# _*_ coding utf-8 _*_
# FILENAME：.py
# DESCRIPTION: 
# AUTH: 
# DATE: 2022/6/82:45 下午
import datetime

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *

from components.BaseMainWindow import BaseMainWindow
from components.MessageBoxComponent import MessageBox
from components.SelectFactoryComponent import SelectFactoryComponent
from constant import CacheType
from models.baseModel import get_session
from models.requireSummaryModel import RequireSummaryModel
from utils.cacheUtils import CacheThread
from views.require.selectBom import SelectBom
from views.require.selectDelayTime import SelectDelayTime
from views.require.selectProduceQueue import SelectProduceQueue
from views.require.selectProduceSpeed import SelectProduceSpeed
from views.requiresummary.selectParts import SelectParts


class SummaryForm(BaseMainWindow):
    __id = 0
    __weldId = None
    __paintId = None
    __summaryId = None
    __factoryId = 0
    __factoryName = None
    __parent = None

    def __init__(self, parent, id, factoryId, factoryName):
        super(SummaryForm, self).__init__()
        self.__id = id
        self.__factoryId = factoryId
        self.__factoryName = factoryName
        self.__parent = parent
        self.setupUi(self)

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(550, 400)
        MainWindow.setWindowTitle("零件需求汇总")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        '''水平布局'''
        hbox = QHBoxLayout()
        self.save_btn = QPushButton()
        self.save_btn.setText('汇总')
        self.save_btn.setFixedWidth(80)
        self.save_btn.clicked.connect(self.save_btn_click)

        self.cancel_btn = QPushButton()
        self.cancel_btn.setText('关闭')
        self.cancel_btn.setFixedWidth(80)
        self.cancel_btn.clicked.connect(self.cancel_btn_click)
        hbox.addStretch(1)  # 占位
        hbox.addWidget(self.save_btn)
        hbox.addWidget(self.cancel_btn)
        hbox.addStretch(1)  # 占位
        btnPanelWidget = QWidget()
        btnPanelWidget.setLayout(hbox)

        '''表单布局'''
        self.fac_lab = QLabel()
        self.fac_lab.setText('选择工厂：')
        self.fac_lab1 = QLabel()
        self.fac_lab1.setText(self.__factoryName)
        self.fac_lab1.setFixedHeight(20)

        self.weld_lab = QLabel()
        self.weld_lab.setText('焊装零件：')
        self.weld_edit = QLineEdit()
        self.weld_btn = QPushButton("选择")
        self.weld_btn.clicked.connect(lambda: self.openSelectParts(self, 1))

        self.paint_lab = QLabel()
        self.paint_lab.setText('涂装零件：')
        self.paint_edit = QLineEdit()
        self.paint_btn = QPushButton("选择")
        self.paint_btn.clicked.connect(lambda: self.openSelectParts(self, 2))

        self.summary_lab = QLabel()
        self.summary_lab.setText('总装零件：')
        self.summary_edit = QLineEdit()
        self.summary_btn = QPushButton("选择")
        self.summary_btn.clicked.connect(lambda: self.openSelectParts(self, 3))


        #  布局
        gbox = QGridLayout()
        gbox.addWidget(self.fac_lab, 0, 0, 1, 1)
        gbox.addWidget(self.fac_lab1, 0, 1, 1, 1)

        gbox.addWidget(self.weld_lab, 1, 0, 1, 1)
        gbox.addWidget(self.weld_edit, 1, 1, 1, 1)
        gbox.addWidget(self.weld_btn, 1, 2, 1, 1)

        gbox.addWidget(self.paint_lab, 2, 0, 1, 1)
        gbox.addWidget(self.paint_edit, 2, 1, 1, 1)
        gbox.addWidget(self.paint_btn, 2, 2, 1, 1)

        gbox.addWidget(self.summary_lab, 3, 0, 1, 1)
        gbox.addWidget(self.summary_edit, 3, 1, 1, 1)
        gbox.addWidget(self.summary_btn, 3, 2, 1, 1)

        # gbox.setColumnStretch(3, 1)

        gbox.addWidget(btnPanelWidget, 4, 0, 1, 3)

        vbox = QVBoxLayout()
        vbox.addLayout(gbox)
        vbox.addLayout(hbox)

        self.centralwidget.setLayout(vbox)

        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 550, 26))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        # self.retranslateUi(MainWindow)
        # QtCore.QMetaObject.connectSlotsByName(MainWindow)

    # def retranslateUi(self, MainWindow):
    #     _translate = QtCore.QCoreApplication.translate
    #     MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow1"))


    def save_btn_click(self):
        if self.weld_edit.text().strip() == '' and self.paint_edit.text().strip() == '' and self.summary_edit.text().strip() == '':
            messageBox = MessageBox()
            messageBox.warrning(title="提醒", msg="至少选择焊装/涂装/总装中的一组数据")
            return

        #
        time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        session = get_session()
        if self.__id > 0:
            # 编辑
            params = {
                'weld_version_id': self.__weldId,
                'paint_version_id': self.__paintId,
                'summary_version_id': self.__summaryId,
                'update_date': time
            }
            # 更新
            session.query(RequireSummaryModel).filter(RequireSummaryModel.id == self.__id).update(params)
            # 提交&关闭
            session.commit()
            session.close()
            # 缓存数据
            cache_thread = CacheThread(CacheType.REQUIRE_SUMMARY.value, self.__id)
            cache_thread.start()
            # 刷新列表
            self.__parent.query_click()
        #
        self.close()

    def cancel_btn_click(self):
        self.close()

    # flag 1=焊装；2=涂装；3=总装
    def openSelectParts(self, parent, flag=0):
        self.selectParts = SelectParts(self, self.__factoryId, flag)
        self.selectParts.show()

    def setWeldId(self, id):
        self.__weldId = id

    def setPaintId(self, id):
        self.__paintId = id

    def setSummaryId(self, id):
        self.__summaryId = id